<?php
/**
 * Created by PhpStorm.
 * User: yxs
 * Date: 2017/6/5
 * Time: 17:23
 */

namespace model;

use think\Db;
use think\Model;

class ShopGoods extends BaseModel
{
    public function ShopStore()
    {
        return $this->hasOne('ShopStore','id',"store_id");
    }

    protected $resultSetType = 'collection';

    protected $insert = ['add_time', 'putaway_time'];

    public function setAddTimeAttr()
    {
        return time();
    }

    public function setPutawayTimeAttr()
    {
        return time();
    }

    public function getCoverImageAttr($value)
    {
        if (is_numeric($value)){
            $filename = Db::name('file')->where(['id' => $value])->value('filename');
            if ($filename){
                return '/public/admin/static/upload/' . $filename;
            }
        }
        return $value;
    }

    public function getSlideImageAttr($value)
    {
        $slide_url = "";
        if ($value) {
            $slide_id_arr = explode(',', $value);
            foreach ($slide_id_arr as $item) {
                if (is_numeric($item)){
                    $slide_item = Db::name('file')->where(['id' => $item])->value('filename');
                    if ($slide_item){
                        if ($slide_url == "") {
                            $slide_url = '/public/admin/static/upload/' . $slide_item;
                        } else {
                            $slide_url .= ',/public/admin/static/upload/' . $slide_item;
                        }
                    }
                }
            }
            if ($slide_url!=""){
                $value = $slide_url;
            }
        }
        return $value;
    }
}